package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.crm.CrmActivity;
import com.nuoniu.sibanyun.entity.crm.CrmForm;
import com.nuoniu.sibanyun.entity.crm.CrmFormField;
import com.nuoniu.sibanyun.mapper.crm.CrmFormFieldMapper;
import com.nuoniu.sibanyun.service.crm.ICrmActivityService;
import com.nuoniu.sibanyun.service.crm.ICrmFormFieldService;
import com.nuoniu.sibanyun.service.crm.ICrmFormService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 表单字段管理 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-09-06
 */
@Service
public class CrmFormFieldServiceImpl extends ServiceImpl<CrmFormFieldMapper, CrmFormField> implements ICrmFormFieldService {

    @Autowired
    private ICrmFormService crmFormService;
    @Autowired
    private ICrmActivityService activityService;

    @Override
    public void saveCrmFiled(CrmForm crmForm) {
        UserInfo userInfo = UserThreadLocal.get();
        List<Long> detailIds = new ArrayList<>();
        List<CrmFormField> formFields = crmForm.getFields();

        if(crmForm.getId() == null) {
            CrmActivity activity = activityService.getById(crmForm.getActivityId());
            crmForm.setBeginDate(activity.getStartTime());
            crmForm.setEndDate(activity.getEndTime());
            crmForm.setCompanyId(userInfo.getCompanyId());
            crmForm.setCreateUserId(userInfo.getUserId());
            crmForm.setCreateUserName(userInfo.getRealName());
        }

        //保存表单
        crmFormService.saveOrUpdate(crmForm);

        List<CrmFormField> fields = new ArrayList<>();
        formFields.forEach(f -> {
            if(f.getName() == null || f.getType() == null) {
                return;
            }

            if(f.getId() == null) {
                f.setCompanyId(userInfo.getCompanyId());
                f.setCompanyName(userInfo.getCompanyName());
                f.setCreateUserId(userInfo.getUserId());
                f.setCreateUserName(userInfo.getRealName());
                f.setFormId(crmForm.getId().intValue());
            }else {
                detailIds.add(f.getId());
            }

            fields.add(f);
        });

        //删除多余字段
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.eq("form_id", crmForm.getId());
        if(detailIds != null && detailIds.size() > 0) {
            wrapper.notIn("id", detailIds);
        }
        this.remove(wrapper);

        //保存字段
        this.saveOrUpdateBatch(fields);
    }

    @Override
    public List<CrmFormField> getFormFieldList(Long formId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("form_id", formId);
        return list(wrapper);
    }
}
